package sprites;

import java.awt.Point;
import view.Console;

/**
 * Class of sprites not controlled by user input.
 * @author Amay
 *
 */
public class NonResponsiveSprite extends Sprite {

    private int myDX;
    private int myDY;
    private int mySpeed;
    private String myDirection;

    private final String NORTH = "north";
    private final String SOUTH = "south";
    private final String EAST = "east";
    private final String WEST = "west";

    /**
     * Constructor that calls super class.
     */
    public NonResponsiveSprite () {
        super();
    }

    /**
     * Constructor that sets sprite image from String image location.
     * @param imgLoc String location of sprite image.
     */
    public NonResponsiveSprite (String imgLoc) {
        super(imgLoc);
    }

    /**
     * Constructor that creates a new Sprite of the same parameters as the passed-in Sprite.
     * Also sets movement to mimic that of nonRespSprite
     * @param nonRespSprite NonResponsiveSprite to be copied
     */
    public NonResponsiveSprite (NonResponsiveSprite nonRespSprite) {
        super(nonRespSprite);
        configureMovement(nonRespSprite.mySpeed, nonRespSprite.myDirection);
    }

    @Override
    public void update () {
        updatePosition();
        Point currentPos = getPosition();
        currentPos.setLocation(currentPos.getX() + myDX, currentPos.getY() + myDY);
        configurePosition(currentPos);
    }

    @Override
    public void collisionHandle (Sprite s) {
        // Does nothing if collides with another sprite
    }

    @Override
    public boolean isValid() {
        boolean isAlive = super.isValid();
        boolean isInBounds = true;
        if (WEST.equalsIgnoreCase(myDirection)) {
            isInBounds =  getPosition().getX() > 0;
        }
        if (EAST.equalsIgnoreCase(myDirection)) {
            isInBounds =  getPosition().getX() < Console.getConsoleWidth();
        }
        if (NORTH.equalsIgnoreCase(myDirection)) {
            isInBounds = getPosition().getY() > 0;
        }
        if (SOUTH.equalsIgnoreCase(myDirection)) {
            isInBounds = getPosition().getY() < Console.getConsoleHeight();
        }

        return isInBounds && isAlive;
    }

    /**
     * Configure the speed and direction this sprite moves in.
     * @param speed int that represents speed of movement
     * @param direction int that represents direction of movement
     */
    public void configureMovement (int speed, String direction) {
        mySpeed = speed;
        myDirection = direction;
    }

    private void updatePosition () {
        myDX = 0;
        myDY = 0;
        if (WEST.equalsIgnoreCase(myDirection)) {
            myDX = -mySpeed;
        }
        if (EAST.equalsIgnoreCase(myDirection)) {
            myDX = mySpeed;
        }
        if (NORTH.equalsIgnoreCase(myDirection)) {
            myDY = -mySpeed;
        }
        if (SOUTH.equalsIgnoreCase(myDirection)) {
            myDY = mySpeed;
        }
    }
}
